package com.xuef.controller;

import com.xuef.base.ApiResponse;
import com.xuef.entity.Catalog;
import com.xuef.entity.User;
import com.xuef.service.CatalogService;
import com.xuef.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 */
@Controller
@RequestMapping("/catalogs")
public class CatalogController {
	
	@Autowired
	private CatalogService catalogService;
	
	@Autowired
	private UserDetailsService userDetailsService;
	/**
	 * 获取分类列表
	 * @param model
	 * @return
	 */
	@GetMapping
	public String listCatalogs(@RequestParam(value="username",required=true) String username,
							   Model model,
							   Authentication authentication) {
		User user = (User)userDetailsService.loadUserByUsername(username);
		List<Catalog> catalogs = catalogService.listCatalogs(user);

		// 判断操作用户是否是分类的所有者
		boolean isOwner = false;
		if (authentication !=null && authentication.isAuthenticated()
				 &&  !authentication.getPrincipal().toString().equals("anonymousUser")) {
			User principal = (User)authentication.getPrincipal();
			if (principal != null && user.getUsername().equals(principal.getUsername())) {
				isOwner = true;
			} 
		} 
		model.addAttribute("isCatalogsOwner", isOwner);
		model.addAttribute("catalogs", catalogs);

		// ??? 模板引擎什么时候渲染页面???
		return "userspace/u :: #catalogReplace";
	}
	/**
	 * 保存新增分类
	 * @return
	 */
	@PostMapping
	@PreAuthorize("authentication.name.equals(#catalogVO.username)")// 指定用户才能操作方法
	public ResponseEntity<ApiResponse> create(@RequestBody CatalogVO catalogVO) {
		String username = catalogVO.getUsername();
		Catalog catalog = catalogVO.getCatalog();
		
		User user = (User)userDetailsService.loadUserByUsername(username);
		catalog.setUserId(user.getId());
		catalog.setUser(user);
		catalogService.saveCatalog(catalog);

		return ResponseEntity.ok().body(ApiResponse.ofSuccess(catalog));
	}
	
	/**
	 * 删除分类
	 * @return
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("authentication.name.equals(#username)")  // 指定用户才能操作方法
	public ResponseEntity<ApiResponse> delete(String username, @PathVariable("id") Integer id) {
		catalogService.removeCatalog(id);

		return ResponseEntity.ok().body(ApiResponse.ofSuccess(id));
	}
	
	/**
	 * 获取分类编辑界面
	 * @param model
	 * @return
	 */
	@GetMapping("/edit")
	public String getCatalogEdit(Model model) {
		Catalog catalog = new Catalog(null, null);
		model.addAttribute("catalog",catalog);
		// catalogedit.html 会被填充进弹出的模态框中
		return "userspace/catalogedit";
	}
	/**
	 * 根据 Id 获取分类
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	public String getCatalogById(@PathVariable("id") Integer id, Model model) {
		Catalog catalog = catalogService.getCatalogById(id);
		model.addAttribute("catalog",catalog);
		return "userspace/catalogedit";
	}
}
